import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';

class DkCenterDialog {
  final CenterDialogOption tipDialogOption;

  DkCenterDialog.create(this.tipDialogOption);

  DkCenterDialog show(BuildContext context) {
    var future = showDialog(
        context: context,
        barrierDismissible: tipDialogOption.barrierDismissible,
        builder: (BuildContext context) {
          List<Widget> list = List();
          list.add(Expanded(
            flex: 1,
            child: GestureDetector(
              onTap: () {
                if (tipDialogOption.barrierDismissible) {
                  Navigator.of(context).pop(false);
                }
              },
              child: Container(
                color: Colors.transparent,
              ),
            ),
          ));

          list.add(tipDialogOption.widgetBuilder(context));

          list.add(Expanded(
            flex: 1,
            child: GestureDetector(
              onTap: () {
                if (tipDialogOption.barrierDismissible) {
                  Navigator.of(context).pop(false);
                }
              },
              child: Container(
                color: Colors.transparent,
              ),
            ),
          ));
          return Container(
            color: Colors.transparent,
            child: Column(
              children: list,
            ),
          );
        });
    if (null != tipDialogOption.handleResult && null != future) {
      future.then((value) {
        tipDialogOption.handleResult(value);
      });
    }
    return this;
  }
}

class CenterDialogOption {
  HandleResult<bool> handleResult;
  bool barrierDismissible;
  WidgetBuilder widgetBuilder;

  CenterDialogOption(this.widgetBuilder,
      {this.handleResult, this.barrierDismissible}) {
    barrierDismissible ??= true;
  }
}
